package bp.sys;

import bp.da.*;
import bp.en.*;
import bp.en.Map;
import bp.difference.*;
import bp.tools.SystemUtil;
import bp.web.WebUser;
import bp.wf.port.WFEmp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


/** 
 用户日志
*/
public class UserLog extends EntityMyPK
{
	@Override
	public UAC getHisUAC() {
		UAC uac = new UAC();
		uac.Readonly();
		return uac;
	}

	///#region 基本属性
	public final String getIP()  {
		return this.GetValStringByKey(UserLogAttr.IP);
	}
	public final void setIP(String value){
		this.SetValByKey(UserLogAttr.IP, value);
	}

	public final String getLocatinon()  {
		return this.GetValStringByKey(UserLogAttr.Locatinon);
	}
	public final void setLocatinon(String value){
		this.SetValByKey(UserLogAttr.Locatinon, value);
	}

	public final String getBrower()  {
		return this.GetValStringByKey(UserLogAttr.Brower);
	}
	public final void setBrower(String value){
		this.SetValByKey(UserLogAttr.Brower, value);
	}


	public final String getOS()  {
		return this.GetValStringByKey(UserLogAttr.OS);
	}
	public final void setOS(String value){
		this.SetValByKey(UserLogAttr.OS, value);
	}


	/** 
	 日志标记键
	*/
	public final String getLogFlag()  {
		return this.GetValStringByKey(UserLogAttr.LogFlag);
	}
	public final void setLogFlag(String value){
		this.SetValByKey(UserLogAttr.LogFlag, value);
	}
	/** 
	 FK_Emp
	*/
	public final String getEmpNo()  {
		return this.GetValStringByKey(UserLogAttr.EmpNo);
	}
	public final void setEmpNo(String value){
		this.SetValByKey(UserLogAttr.EmpNo, value);
	}
	public final String getEmpName()  {
		return this.GetValStringByKey(UserLogAttr.EmpName);
	}
	public final void setEmpName(String value){
		this.SetValByKey(UserLogAttr.EmpName, value);
	}
	public final String getRDT()  {
		return this.GetValStringByKey(UserLogAttr.RDT);
	}
	public final void setRDT(String value){
		this.SetValByKey(UserLogAttr.RDT, value);
	}

	public final String getDocs()  {
		return this.GetValStringByKey(UserLogAttr.Docs);
	}
	public final void setDocs(String value){
		this.SetValByKey(UserLogAttr.Docs, value);
	}


		///#endregion


		///#region 构造方法
	/** 
	 用户日志
	*/
	public UserLog()
	{
	}

	/** 
	 EnMap
	*/
	@Override
	public Map getEnMap()
	{
		if (this.get_enMap() != null)
		{
			return this.get_enMap();
		}
		Map map = new Map("Sys_UserLogT", "用户日志");
		map.AddMyPK();
		map.AddTBString(UserLogAttr.EmpNo, null, "用户账号", true, true, 0, 50, 20);
		map.AddTBString(UserLogAttr.EmpName, null, "用户名", true, true, 0, 30, 20);
		map.AddTBString(UserLogAttr.RDT, null, "记录日期", true, true, 0, 20, 20);
		map.AddTBString(UserLogAttr.IP, null, "IP", true, true, 0, 200, 20);
		map.AddTBString(UserLogAttr.Locatinon, null, "位置", true, true, 0, 200, 100);
		map.AddTBString(UserLogAttr.Brower, null, "浏览器", true, true, 0, 100, 100);
		map.AddTBString(UserLogAttr.OS, null, "操作系统", true, true, 0, 100, 100);
		map.AddTBStringDoc(UserLogAttr.Docs, null, "说明", true, true, true);

		//map.AddDDLEntities(UserLogAttr.LogFlag, null, "类型", new UserLogTypes(), false);
		//map.AddDDLEntities(UserLogAttr.Level, null, "级别", new UserLogLevels(), false);
		map.AddTBString(UserLogAttr.LogFlag, null, "类型", true, true, 0, 200, 20);
		map.AddTBString(UserLogAttr.LevelText, null, "级别", true, true, 0, 200, 20);


		map.DTSearchKey = UserLogAttr.RDT;
		map.DTSearchWay = DTSearchWay.ByDate;

		//查询条件.
		map.AddSearchAttr(UserLogAttr.LevelText);
		map.AddSearchAttr(UserLogAttr.LogFlag);


		this.set_enMap(map);
		return this.get_enMap();
	}

		///#endregion

	@Override
	protected boolean beforeInsert() throws Exception {
		//是否开启日志记录
		String IsEnableUserLog =SystemConfig.IsEnableUserLog();
		if (IsEnableUserLog.equals("0") && !this.getLogFlag().equals("登录")
				&& !this.getLogFlag().equals("退出") && !this.getLogFlag().contains("删除")) {
			return false;
		}

		this.setMyPK(DBAccess.GenerGUID());
		this.setRDT(DataType.getCurrentDateTime());
		this.setEmpNo(WebUser.getNo());
		this.setEmpName(WebUser.getName());
		if (SystemConfig.isBSsystem()) {
			this.setIP(bp.difference.Glo.getIP());
		}
		if (this.getLogFlag().equals("登录")	|| this.getLogFlag().equals("退出")
				|| this.getLogFlag().equals("访问") || this.getLogFlag().contains("删除") )
		{
			if (this.getLogFlag().equals("访问") )
			{
				this.setEmpNo("访客");
				this.setEmpName("访客");
			}else{
				this.setDocs(WebUser.getToken());
			}
			//退出时
			// 1.宽泛模式，根据toke 删除 port_token 对应数据
			//2.严谨模式时，根据no 修改  wf_emp 表的Atpara 字段 online=0
			if (this.getLogFlag().equals("退出") )
			{
				//严谨模式,saas模式只有宽泛模式
				if (SystemConfig.getTokenModel() == 1 && SystemConfig.getCCBPMRunModel() != CCBPMRunModel.SAAS) {
					WFEmp emp = new WFEmp(WebUser.getNo());
					emp.SetPara("Online", "0");
					emp.Update();
				}else{
					String sql="delete from port_token where mypk='"+WebUser.getToken()+"'";
                    DBAccess.RunSQL(sql);
                }
            }else{ //访客，登录，删除 记录ip详情
				try {
					String jsonString = SystemUtil.GetCityByIp("");
					// 将 JSON 字符串转换为对象
					JSONObject json= JSON.parseObject(jsonString);
					this.setIP(json.getString("ip"));
					this.setLocatinon(json.getString("pro")+" "+json.getString("city"));
					this.setBrower(SystemUtil.GetBrowser());
					this.setOS(SystemUtil.GetOperatingSystem());
				}catch(Exception E)
				{

				}
			}
		}
		return super.beforeInsert();
	}


		///#region 重写
	@Override
	public Entities GetNewEntities()
	{
		return new UserLogs();
	}

		///#endregion 重写
}
